package com.wn.inventory.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.common.ActivitiStartEntity;
import com.wn.common.PageBean;
import com.wn.common.ResponseEntity;
import com.wn.entity.Hr;
import com.wn.entity.PurchaseInWarehouseApplication;
import com.wn.inventory.feign.ActivitiFeignClient;
import com.wn.inventory.mapper.PurchaseInWarehouseApplicationMapper;
import com.wn.inventory.mapper.PurchaseListMapper;
import com.wn.inventory.service.PurchaseInWarehouseApplicationService;
import com.wn.inventory.vo.PurchaseInWarehouseApplicationVo;
import com.wn.utils.CurrentTime;
import com.wn.utils.PageBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ author     ：NST
 * @ date       ：Created in 2022/10/21 9:35
 * @ description：采购入库申请表Impl
 * @ modified By：
 * @ version:
 */
@Service
public class PurchaseInWarehouseApplicationImpl implements PurchaseInWarehouseApplicationService {
    @Autowired
    PurchaseInWarehouseApplicationMapper purchaseInWarehouseApplicationMapper;
    @Autowired
    PurchaseListMapper purchaseListMapper;
    @Autowired
    ActivitiFeignClient activitiFeignClient;
    /**
     * @param purchaseInWarehouseApplication
     * @return void
     * @ author nst
     * @ date 2022/10/21 9:38
     * @ Description 增加采购入库信息
     */
    @Override
    @Transactional
    public void add(PurchaseInWarehouseApplication purchaseInWarehouseApplication, HttpServletRequest request) {
        purchaseInWarehouseApplication.setApplicant(new Hr(Integer.parseInt(request.getHeader("userId"))));
        String currentTime = CurrentTime.getCurrentTime();
        purchaseInWarehouseApplication.setInWarehouseTime(currentTime);
        String identifier="PURIWA"+CurrentTime.getCurrentTimeString();
        purchaseInWarehouseApplication.setIdentifier(identifier);
        ResponseEntity<String> entity = activitiFeignClient.startInstance(new ActivitiStartEntity("in_warehouss_application_id", "user1", request.getHeader("userWorkNumber")));
//        activitiFeignClient.completeTask(new ActivitiCompleteEntity("in_warehouss_application_id",entity.getData(),"user1",));
        purchaseInWarehouseApplication.setActivitiInstanceId(entity.getData());
        purchaseInWarehouseApplicationMapper.insert(purchaseInWarehouseApplication);
        purchaseListMapper.insert(purchaseInWarehouseApplication.getPurchaseLists());
    }

    @Override
    public PageBean<PurchaseInWarehouseApplication> getByCondition(PurchaseInWarehouseApplicationVo purchaseInWarehouseApplicationVo) {
        Page<PurchaseInWarehouseApplication> page = PageHelper.startPage(purchaseInWarehouseApplicationVo.getCurrPage(), purchaseInWarehouseApplicationVo.getPageSize());
        List<PurchaseInWarehouseApplication> list = purchaseInWarehouseApplicationMapper.getByCondition(purchaseInWarehouseApplicationVo.getPurchaseInWarehouseApplication());
        return PageBeanUtil.getPageBean(list,page,purchaseInWarehouseApplicationVo.getCurrPage(),purchaseInWarehouseApplicationVo.getPageSize());
    }
}
